package org.xilin

class CourseController {
    
    def authenticateService
    def semesterService
    
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list")
    }

    def list = {
        log.debug("list")
        println "active semester =" + session.activeSemester
        def courseList = semesterService.getListBySemester(Course)
        render(template: "courseList", model: [instanceList: courseList, courseInstanceTotal: courseList.size()])
    }

    def create = {
        log.debug("create")
        def courseInstance = Course.get(params.id)
        if (!courseInstance) {
            courseInstance = new Course()
        }
        else {
            courseInstance.properties = params
        }
        render(template: "create", model: [courseInstance: courseInstance])
    }

    def save = {
        log.debug("save")
        println "session.activeSemester = " + session.activeSemester
        def courseInstance = null
        if (params.id) {
            courseInstance = Course.get(params.id)
            courseInstance.properties = params
        }
        else {
            courseInstance = new Course(params)
        }

        if (!courseInstance.validate()) {
            courseInstance.errors.allErrors.each {
                log.debug(it)
            }
            flash.message = "Missing required data"
            render(template: params.view, model: [courseInstance: courseInstance])
            return
        }

        if (!courseInstance.save(flush: true)) {
            courseInstance.errors.allErrors.each {
                println it
            }
            //flash.message = "${message(code: 'default.saved.message', args: [message(code: 'course.label', default: 'Course'), courseInstance.id])}"
        }
        //render(template: params.view, model: [courseInstance: courseInstance])
        redirect(action: "list")
    }

    def show = {
        def courseInstance = Course.get(params.id)
        if (!courseInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "show", model: [courseInstance: courseInstance])
        }
    }

    def delete = {
        def courseInstance = Course.get(params.id)
        if (courseInstance) {
            try {
                //Teacher.getProperties().each {
                //    println it
                //}
                //Teacher.metaClass.getMetaMethods().each {
                //    println it
                //}
                def teacher = Teacher.findByCourse(courseInstance)
                println "teacher=" + teacher
                if (teacher) {
                    flash.message = "Cannot delete class" + courseInstance + ". It is referenced by teacher '" + teacher + "'"
                }
                else {
                    courseInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'course.label', default: 'Course'), params.id])}"
                }
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'course.label', default: 'Course'), params.id])}"
                redirect(action: "list")
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), params.id])}"
            redirect(action: "list")
        }
    }
}
